package com.situ.meetingRoom.service.impl;

import com.situ.meetingRoom.dao.BookingDao;
import com.situ.meetingRoom.dao.DeptDao;
import com.situ.meetingRoom.dao.UserDao;
import com.situ.meetingRoom.dao.impl.BookingDaoImpl;
import com.situ.meetingRoom.dao.impl.DeptDaoImpl;
import com.situ.meetingRoom.dao.impl.UserDaoImpl;
import com.situ.meetingRoom.entity.Booking;
import com.situ.meetingRoom.entity.Dept;
import com.situ.meetingRoom.entity.User;
import com.situ.meetingRoom.service.UserService;
import com.situ.meetingRoom.util.MD5Util;
import com.situ.meetingRoom.util.PageInfo;
import com.situ.meetingRoom.vo.UserVO;

import java.util.List;

public class UserServiceImpl implements UserService {

    private final UserDao userDao = new UserDaoImpl();
    private final DeptDao deptDao = new DeptDaoImpl();

    private static String SALT = "abcd";

    @Override
    public User login(User user) throws Exception {
        //验证账号
        User user1 = userDao.selectById(user.getId());
        if (user1 == null) {
            throw new Exception("工号不存在");
        }
        //密码
        String md5 = MD5Util.getDBMD5(user.getPassword(), SALT);
        if (!user1.getPassword().equals(md5)) {
            throw new Exception("密码错误");
        }
        //状态
        if (user1.getStatus() == 1) {
            throw new Exception("员工已离职");
        }
        //成功
        return user1;
    }

    @Override
    public int add(User user) throws Exception {
//        保证那唯一的字段不能重复
        User user1 = userDao.selectTel(user.getTel());
        Dept dept = deptDao.selectById(user.getDeptId());
        if (user1 != null) {
            //电话已经被使用
            throw new Exception("电话已经被使用");
        }
        if (dept.getStatus() == 1){
            throw new Exception("该部门已被裁撤！");
        }
        //密码要加密
        user.setPassword(MD5Util.getDBMD5(user.getPassword(), SALT));
//        保存到数据库
        return userDao.insert(user);
    }

    @Override
    public int remove(Integer id) throws Exception {
        User user = userDao.selectById(id);
        //员工不存在
        if (user == null) {
            throw new Exception("员工不存在");
        }
        //员工状态
        if (user.getStatus() == 1) {
            throw new Exception("员工已离职");
        }
        BookingDao bd = new BookingDaoImpl();
        Booking booking = bd.selectUserId(user.getId());

        if (booking != null && booking.getStatus() == 0){
            throw new Exception("有预约记录，无法删除");
        }
        //设置员工状态
        user.setStatus(1);
        //更新到数据库
        return userDao.update(user);
    }

    @Override
    public int edit(User user) throws Exception {
        //有效性 电话不能冲突
        User user1 = userDao.selectTel(user.getTel());
        Dept dept = deptDao.selectById(user.getDeptId());
        if (dept.getStatus() == 1){
            throw new Exception("该部门已被裁撤！");
        }
        if (user1 != null && !user1.getId().equals(user.getId())) {
            throw new Exception("电话已被使用");
        }
        //部门ID验证
        return userDao.update(user);

    }

    @Override
    public User getById(Integer id) {
        return userDao.selectById(id);
    }

    @Override
    public List<UserVO> getAll(User user) {
        return userDao.selectAll(user);
    }

    @Override
    public int resetPwd(String oldPwd, String newPwd, Integer id) throws Exception {

        //        新密码与旧密码不能一致
        if (oldPwd.equals(newPwd)) {
            throw new Exception("新旧密码不能一样！");
        }

        //判断旧密码是否正确
        User sUser = userDao.selectById(id);
        String md5Pwd = MD5Util.getDBMD5(oldPwd, SALT);
        if (! md5Pwd.equals(sUser.getPassword())) {
            throw new Exception("旧密码错误");
        }


        //新密码加密
        newPwd = MD5Util.getDBMD5(newPwd, SALT);
        sUser.setPassword(newPwd);
        //修改数据库
        return userDao.update(sUser);
    }

    @Override
    public PageInfo getForPage(Integer page, Integer limit, User user) {
        return userDao.selectForPage(page,limit ,user);
    }
}
